﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Guru.Web.UI;
using Guru.Data;

public partial class UserControl_Customer : GuruUserControl
{
    public int GuestID
    {
        get 
        {
            if (ViewState["GuestID"] == null) return 0;
            else return int.Parse(ViewState["GuestID"].ToString());
        }
        set         
        {
            ViewState["GuestID"] = value;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            this.uxGender.BindComboBox(HSMConstants.RefType.Gender);
            this.uxCustomerType.BindComboBox(HSMConstants.RefType.GuestType);
        }
    }   

    public int Save()
    {
        string keywords = string.Format("{0} {1} {2} {3}",
            this.uxCustomerName.Text,
            this.uxCustomerName.Text.RemoveVietnamese(),
            this.uxAddress.Text,
            this.uxAddress.Text.RemoveVietnamese());

        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@GuestType", this.uxCustomerType.SelectedValue, DbType.Int32);
        parameters.Add("@GuestName", this.uxCustomerName.Text, DbType.String);
        parameters.Add("@Gender", this.uxGender.SelectedIndex, DbType.Int32);
        parameters.Add("@Address", this.uxAddress.Text, DbType.String);
        parameters.Add("@Phone", this.uxPhone.Text, DbType.String);
        parameters.Add("@Email", this.uxEmail.Text, DbType.String);
        parameters.Add("@Nationality", this.uxCustomerType.SelectedValue, DbType.Int32);
        parameters.Add("@IDNumber", this.uxSocialID.Text, DbType.String);       
        parameters.Add("@VisaNumber",this.uxPassport.Text, DbType.String);
        if (this.uxPassportEndDate.SelectedDate != null)
        {
            parameters.Add("@VisaExpiredDate", this.uxPassportEndDate.SelectedDate.Value, DbType.Date);
        }
        parameters.Add("@Keywords", keywords, DbType.String);

        if (this.GuestID <= 0)
        {
            parameters.Add("@ReturnID", 0, DbType.Int32, true);
            HSMCommon.HSMBusiness.UpdateData("spa_HSM_Guest_Insert", parameters, out outParameters);
            this.GuestID = int.Parse(outParameters[0].ParameterValue.ToString());
        }
        else
        {
            parameters.Add("@GuestID",this.GuestID, DbType.Int32);
            HSMCommon.HSMBusiness.UpdateData("spa_HSM_Guest_Update", parameters, out outParameters);
        }
        return this.GuestID;
    }
    public void LoadGuest()
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@GuestID", this.GuestID, DbType.Int32);
        DataTable tbGuest = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Guest_GetByID", parameters);

        this.uxCustomerName.Text = tbGuest.Rows[0]["GuestName"].ToSafeString();
        this.uxPhone.Text = tbGuest.Rows[0]["Phone"].ToSafeString();
        this.uxSocialID.Text = tbGuest.Rows[0]["IDNumber"].ToSafeString();
        this.uxGender.SetComboValue(tbGuest.Rows[0]["Gender"]);
        this.uxAddress.Text = tbGuest.Rows[0]["Address"].ToSafeString();
        this.uxEmail.Text = tbGuest.Rows[0]["Email"].ToSafeString();
        this.uxNationality.Text = tbGuest.Rows[0]["Nationality"].ToSafeString();
        this.uxPassport.Text = tbGuest.Rows[0]["VisaNumber"].ToSafeString();
        this.uxCustomerType.SetComboValue (tbGuest.Rows[0]["GuestType"]);
        this.uxPassportEndDate.SetDatePickerValue(tbGuest.Rows[0]["VisaExpiredDate"]);
    }
}
